package com.kgc.scd.controller;

import com.kgc.scd.annotation.RequestShopUser;
import com.kgc.scd.annotation.TokenPermission;
import com.kgc.scd.async.AsyncSchedule;
import com.kgc.scd.bean.ShopUser;
import com.kgc.scd.service.BushGoodsService;
import com.kgc.scd.util.RedisUtils;
import com.kgc.scd.util.RequestResult;
import com.kgc.scd.util.ResultBuildUtil;
import com.kgc.scd.util.SnowUtil;
import com.kgc.scd.vo.ShopAddressVO;
import com.kgc.scd.vo.ShopGoodsVO;
import com.kgc.scd.vo.ShopUserBushVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Date;

/**
 * Created On : 2022/11/15.
 * <p>
 * Author : Administrator
 * <p>
 * Description: BushGoodsController
 */
@Slf4j
@RestController
@Api(tags = "13、抢购接口(tlx)",value = "登录的用户，对可以抢购的商品进行抢购等记，并通过抢购时间向用户发送抢购提醒短信通知")
public class BushGoodsController {

    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    private BushGoodsService bushGoodsService;

    @Autowired
    private AsyncSchedule asyncSchedule;

    @Autowired
    private SnowUtil snowUtil;

    //@GetMapping("/createTestToken")
    //@ApiOperation(value = "模拟token来源",notes = "仅仅用于测试")
    //@TokenPermission
    //@RequestShopUser ShopUser shopUser
    /*public RequestResult<?> createTestToken(){

        ShopUser shopUser=new ShopUser();
        shopUser.setGender(0);
        shopUser.setId("1043555957562408960");
        shopUser.setIntegration(0);
        shopUser.setMobile("17301020304");
        shopUser.setNickname("123");
        shopUser.setPassword("123456");
        shopUser.setRegisterIp("");
        shopUser.setUserLevelId(0);
        shopUser.setUsername("root");
        shopUser.setWeixinOpenid("123456");
        redisUtils.set("17301020304",shopUser);


        ShopAddressVO  shopAddressVO=new ShopAddressVO();
        shopAddressVO.setId("3");
        shopAddressVO.setName("小明");
        shopAddressVO.setUserId("111");
        shopAddressVO.setMobile("13800008888");
        shopAddressVO.setAddress("民族大道1号");
        shopAddressVO.setStatus("默认");
        redisUtils.set(shopUser.getId()+":address:3",shopAddressVO);
        return ResultBuildUtil.success();
    }*/


    @GetMapping("/preOrder")
    @TokenPermission
    @ApiOperation(value = "抢购登记",notes = "登录的用户，对可以抢购的商品进行抢购等记，并通过抢购时间向用户发送抢购提醒短信通知")
    @ApiResponses({
            @ApiResponse(code = 200,message = "成功"),
            @ApiResponse(code = 999,message = "失败")
    })
    public RequestResult<?> preOrder(String pid, @ApiIgnore @RequestShopUser ShopUser shopUser){
        //判断商品是否是抢购商品
        ShopGoodsVO shopGoodsVO=bushGoodsService.involveServerGoodsIsBush(pid);
        log.info("发布时间:"+shopGoodsVO.getBushStartTime());
        log.info("现在时间:"+(new Date()));
        if(shopGoodsVO==null||shopGoodsVO.getBushStartTime().getTime()<(new Date()).getTime()){
            return ResultBuildUtil.fail("该商品不在抢购中");
        }

        redisUtils.set("qgGoods:"+pid,shopGoodsVO.getGoodsNumber());

        //存入到数据库中
        ShopUserBushVO shopUserBushVO=new ShopUserBushVO();
        shopUserBushVO.setId(snowUtil.snowId());
        shopUserBushVO.setPid(pid);
        shopUserBushVO.setStartTime(shopGoodsVO.getBushStartTime());
        shopUserBushVO.setUserId(shopUser.getId());

        if(!bushGoodsService.involveServerSaveBushDetail(shopUserBushVO)){

            return ResultBuildUtil.fail("登记失败,请重试");
        }

        if(!redisUtils.lock(shopUser.getId()+":"+"goods:qg:"+pid,shopUserBushVO.getStartTime(),shopUserBushVO.getStartTime().getTime()-(new Date().getTime()))){

            return ResultBuildUtil.fail("已经预定该商品，无法持续抢购");
        }

        //异步定时
        asyncSchedule.setAsyncSchedule(shopUser.getId(),shopUserBushVO.getStartTime());
        return ResultBuildUtil.success("抢购登记成功");
    }
}
